package org.bgu.survey;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import javax.naming.NamingException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class Survey
 */
public class Survey extends HttpServlet {
	private static final long serialVersionUID = 1L;
	
//	public static final String ID = "id";
	public static final String TARGET = "target";
	public static final String HOME_TO_WORK_NOW = "home_to_work_now";
	public static final String HOME_TO_WORK_5 = "home_to_work_5";
	public static final String HOME_TO_WORK_10 = "home_to_work_10";
	public static final String VEHICLE_NOW = "vehicle_now";
	public static final String VEHICLE_5 = "vehicle_5";
	public static final String VEHICLE_10 = "vehicle_10";
	public static final String SENIORITY = "seniority";
	public static final String WORK_ADVANCE_METER = "work_advance_meter";
	public static final String MOVED_FLAT = "moved_flat";
	public static final String WORK_CAUSED_MOVE_METER = "work_caused_move_meter";
	public static final String TIME_CHANGED_AFTER_MOVE = "time_changed_after_move";
	public static final String SERCHED_FLAT_CLOSER = "serched_flat_closer";
	public static final String HAD_OPPORTUNITY_MOVE_CLOSER = "had_opportunity_move_closer";
	public static final String PLAN_TO_MOVE = "plan_to_move";
	public static final String CONSIDERED_WORK_CLOSER_TO_HOME_METER = "considered_work_closer_to_home_meter";
	public static final String YOU_EARN_MONEY = "you_earn_money";
	public static final String AGE = "age";
	public static final String SEX = "sex";
	public static final String SPOUSE_WORK_CITY = "spouse_work_city";
	public static final String MARITAL_STATUS = "marital_status";
	public static final String CHILDREN = "children";
	public static final String SALARY = "salary";
	public static final String SCHOOL_YEARS = "school_years";
	public static final String FLAT_NOW = "flat_now";
	public static final String FLAT_5 = "flat_5";
	public static final String FLAT_10 = "flat_10";
	public static final String MISHLAH_YAD = "mishlah_yad";
	public static final String TEL = "tel";
	
	private int target;
	
	private int home_to_work_now; 
	private int home_to_work_5;
	private int home_to_work_10;
	private int vehicle_now;
	private int vehicle_5;
	private int vehicle_10;
	private int seniority;
	private int work_advance_meter;
	private boolean moved_flat;
	private int work_caused_move_meter;
	private int time_changed_after_move;
	private boolean serched_flat_closer;
	private boolean had_opportunity_move_closer;
	private boolean plan_to_move;
	private int considered_work_closer_to_home_meter;
	private boolean you_earn_money;
	private int age;
	private boolean sex;
	private String spouse_work_city;
	private boolean marital_status;
	private int children;
	private int salary;
	private int school_years;
	private int flat_now;
	private int flat_5;
	private int flat_10;
	private int mishlah_yad;
	private String tel;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public Survey() {
        super();
        // TODO Auto-generated constructor stub
    }
    
    private int parseInt(String val) {
    	int res = 0;
    	if (val != null && val.length() > 0 && !val.equalsIgnoreCase("null")) {
    		res = Integer.parseInt(val);
    	}
    	
    	return res;
    }
    
    private boolean parseBoolean(String val) {
    	boolean res = false;
    	if (val != null && val.length() > 0 && !val.equalsIgnoreCase("null")) {
    		res = Boolean.parseBoolean(val.toLowerCase());
    	}
    	
    	return res;
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String target = request.getParameter(TARGET);
		request.getSession().setAttribute(TARGET, target);
		
	  	response.sendRedirect("survey.html");
	}
	
	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

		request.setCharacterEncoding("UTF-8");
		home_to_work_now = parseInt(request.getParameter(HOME_TO_WORK_NOW));
		home_to_work_5 = parseInt(request.getParameter(HOME_TO_WORK_5));
		home_to_work_10 = parseInt(request.getParameter(HOME_TO_WORK_10));
		vehicle_now = parseInt(request.getParameter(VEHICLE_NOW));
		vehicle_5 = parseInt(request.getParameter(VEHICLE_5));
		vehicle_10 = parseInt(request.getParameter(VEHICLE_10));
		seniority = parseInt(request.getParameter(SENIORITY));
		work_advance_meter = parseInt(request.getParameter(WORK_ADVANCE_METER));
		moved_flat = parseBoolean(request.getParameter(MOVED_FLAT));
		work_caused_move_meter = parseInt(request.getParameter(WORK_CAUSED_MOVE_METER));
		time_changed_after_move = parseInt(request.getParameter(TIME_CHANGED_AFTER_MOVE));
		serched_flat_closer = parseBoolean(request.getParameter(SERCHED_FLAT_CLOSER));
		had_opportunity_move_closer = parseBoolean(request.getParameter(HAD_OPPORTUNITY_MOVE_CLOSER));
		plan_to_move = parseBoolean(request.getParameter(PLAN_TO_MOVE));
		considered_work_closer_to_home_meter = parseInt(request.getParameter(CONSIDERED_WORK_CLOSER_TO_HOME_METER));
		you_earn_money = parseBoolean(request.getParameter(YOU_EARN_MONEY));
		age = parseInt(request.getParameter(AGE));
		sex = parseBoolean(request.getParameter(SEX));
		spouse_work_city = request.getParameter(SPOUSE_WORK_CITY);
		marital_status = parseBoolean(request.getParameter(MARITAL_STATUS));
		children = parseInt(request.getParameter(CHILDREN));
		salary = parseInt(request.getParameter(SALARY));
		school_years = parseInt(request.getParameter(SCHOOL_YEARS));
		flat_now = parseInt(request.getParameter(FLAT_NOW));
		flat_5 = parseInt(request.getParameter(FLAT_5));
		flat_10 = parseInt(request.getParameter(FLAT_10));
		mishlah_yad = parseInt(request.getParameter(MISHLAH_YAD));
		tel = request.getParameter(TEL);
		
		Object o = request.getSession().getAttribute(TARGET);
		if (o != null) {
			target = parseInt((String)o);
		}
		
		Connection connection = null;
		try {
	      String userName = "root";
	      String password = "master";
	      String url = "jdbc:mysql://127.0.0.1:3306/teza"; //Vista version
	      Class.forName("com.mysql.jdbc.Driver");
	  	  connection = DriverManager.getConnection (url, userName, password);
			
//	  	  javax.sql.DataSource ds=(javax.sql.DataSource)new javax.naming.InitialContext().lookup("java:comp/env/jdbc/bguteza");
//	  	  connection = ds.getConnection(); 
	  	  
	  	  addNew(connection);
	  	  
	  	  response.sendRedirect("thanks.html");
		}
//		catch (NamingException ne) {
//			ne.printStackTrace();
//		}
//		catch (ClassNotFoundException cnfe) {
//			cnfe.printStackTrace();
//		}
		catch (SQLException sqle) {
			sqle.printStackTrace();
		}
		catch (Exception e) {
			e.printStackTrace();
		}
		finally {
			if (connection != null) {
				try {
					connection.close();
				}
				catch (SQLException sqle) {
					sqle.printStackTrace();
				}
			}
		}
	}
	
	private void addNew(Connection connection) throws SQLException {
		Statement stmt = connection.createStatement();

		synchronized(Survey.class) {
			try {
//				java.util.Date date = new java.util.Date();
				StringBuffer query = new StringBuffer();
				query.append("INSERT INTO survey (");
				query.append(TARGET); 
				query.append(",");
				query.append(HOME_TO_WORK_NOW); 
				query.append(",");
				query.append(HOME_TO_WORK_5);
				query.append(",");
				query.append(HOME_TO_WORK_10);
				query.append(",");
				query.append(VEHICLE_NOW);
				query.append(",");
				query.append(VEHICLE_5);
				query.append(","); 
				query.append(VEHICLE_10);
				query.append(",");
				query.append(SENIORITY);
				query.append(",");
				query.append(WORK_ADVANCE_METER);
				query.append(",");
				query.append(MOVED_FLAT);
				query.append(",");
				query.append(WORK_CAUSED_MOVE_METER);
				query.append(",");
				query.append(TIME_CHANGED_AFTER_MOVE);
				query.append(",");
				query.append(SERCHED_FLAT_CLOSER);
				query.append(",");
				query.append(HAD_OPPORTUNITY_MOVE_CLOSER);
				query.append(",");
				query.append(PLAN_TO_MOVE);
				query.append(",");
				query.append(CONSIDERED_WORK_CLOSER_TO_HOME_METER);
				query.append(",");
				query.append(YOU_EARN_MONEY);
				query.append(",");
				query.append(AGE);
				query.append(",");
				query.append(SEX);
				query.append(",");
				query.append(SPOUSE_WORK_CITY);
				query.append(",");
				query.append(MARITAL_STATUS);
				query.append(",");
				query.append(CHILDREN);
				query.append(",");
				query.append(SALARY);
				query.append(",");
				query.append(SCHOOL_YEARS);
				query.append(",");
				query.append(FLAT_NOW);
				query.append(",");
				query.append(FLAT_5);
				query.append(",");
				query.append(FLAT_10);
				query.append(",");
				query.append(MISHLAH_YAD);
				query.append(",");
				query.append(TEL);
				query.append(") ");
				
				query.append("VALUES (");
				query.append(target);
				query.append(",");
				query.append(home_to_work_now);
				query.append(",");
				query.append(home_to_work_5);
				query.append(",");
				query.append(home_to_work_10);
				query.append(",");
				query.append(vehicle_now);
				query.append(",");
				query.append(vehicle_5);
				query.append(",");
				query.append(vehicle_10);
				query.append(",");
				query.append(seniority);
				query.append(",");
				query.append(work_advance_meter);
				query.append(",");
				query.append(moved_flat);
				query.append(",");
				query.append(work_caused_move_meter);
				query.append(",");
				query.append(time_changed_after_move);
				query.append(",");
				query.append(serched_flat_closer);
				query.append(",");
				query.append(had_opportunity_move_closer);
				query.append(",");
				query.append(plan_to_move);
				query.append(",");
				query.append(considered_work_closer_to_home_meter);
				query.append(",");
				query.append(you_earn_money);
				query.append(",");
				query.append(age);
				query.append(",");
				query.append(sex);
				query.append(",'");
				query.append(spouse_work_city);
				query.append("',");
				query.append(marital_status);
				query.append(",");
				query.append(children);
				query.append(",");
				query.append(salary);
				query.append(",");
				query.append(school_years);
				query.append(",");
				query.append(flat_now);
				query.append(",");
				query.append(flat_5);
				query.append(",");
				query.append(flat_10);
				query.append(",");
				query.append(mishlah_yad);
				query.append(",'");
				query.append(tel);
				query.append("');");

				stmt.executeUpdate(query.toString());
			} catch (SQLException sqle) {
				sqle.printStackTrace();
			} finally {
				if (stmt != null) {
					stmt.close();
				}
			}
		}
	}

}
